package com.itheima.tlais_web_management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlais_web_management.mapper.ClassMapper;
import com.itheima.tlais_web_management.mapper.EmpMapper;
import com.itheima.tlais_web_management.pojo.Class;
import com.itheima.tlais_web_management.pojo.Dept;
import com.itheima.tlais_web_management.pojo.Emp;
import com.itheima.tlais_web_management.pojo.PageBean;
import com.itheima.tlais_web_management.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private EmpMapper empMapper;

    @Override
    public void add(Class clas) {
        clas.setCreateTime(LocalDateTime.now());
        clas.setUpdateTime(LocalDateTime.now());
        classMapper.insert(clas);
    }

    @Override
    public void delete(List<Integer> ids) {
        classMapper.delete(ids);
    }

    @Override
    public PageBean list(Integer page, Integer pageSize, String name, LocalDate startTime, LocalDate endTime) {
        //设置分页参数
        Page<Object> startPage = PageHelper.startPage(page, pageSize);

        //执行查询
        List<Class> list = classMapper.list(name,startTime,endTime);
        for (Class c: list) {
            Integer headTeacher = c.getHeadTeacher();
            Emp emp = empMapper.getById(headTeacher);
            c.setEmp(emp);
        }
        Page<Class> emplist = (Page<Class>) list;

        //封装一个 pageBean 对象
        PageBean pageBean = new PageBean(emplist.getTotal(),emplist.getResult());
        return pageBean;
    }

    @Override
    public List<Class> listAllclass() {
        return classMapper.listAllclass();
    }

    @Override
    public void update(Class clas) {
            clas.setUpdateTime(LocalDateTime.now());
            classMapper.update(clas);
    }

    @Override
    public List<Class> getbyclassid(Integer id) {
        return classMapper.getbyclassid(id);
    }


}
